/*
 * Copyright (c) 2015, Potevio Information Technology Co., Ltd. 
 * All rights reserved.
 * POTEVIO PROPRIETARY/CONFIDENTIAL.
 */
package com.cpit.platform.common.entity;

import com.cpit.platform.common.dao.SimpleExpression;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 表格查询的分页条件封装。该结构为jqGrid自动下发，不需在代码中手动构造。
 * @author zhanghaibin
 * @date 2016-01-05
 */
public class TableDataQueryCondition<T> {
	
	/** 一页获取的行数 */
	private String rows = "10";
	
	// 获取的页码
	private String page = "1";
	
	// 获取的第一条记录的索引
	private String startInx = "0";
	
	// 排序列名
	private String sidx = "";
	
	// 其他查询条件
	@SuppressWarnings("rawtypes")
	private Map queryCond = new HashMap();

	// 其他查询条件
	@SuppressWarnings("rawtypes")
	private List<SimpleExpression> queryCondSimple =  new ArrayList<SimpleExpression>();

	// 排序方法asc desc
	private String sord = "asc";
	
	// 其他业务对象查询条件
	private T otherCond = null;

	/**
	 * 通过TableDataQueryCondition获取PageRequest对象，用于构造分页查询条件
	 * @return PageRequest对象
	 */
	public PageRequest getPageRequest() {
		if(this.getSidx() == null || this.getSidx().length() == 0) {
			this.setSidx("id");
		}
		Sort.Direction der = Sort.Direction.ASC.name().equalsIgnoreCase(this.getSord()) ? Sort.Direction.ASC : Sort.Direction.DESC;

		return new PageRequest(this.getPageNumber() - 1, this.getRowsNumber(), new Sort(der, this.getSidx()));
	}

    /**
     * 通过TableDataQueryCondition获取PageRequest对象，用于构造分页查询条件
     * @param cond TableDataQueryCondition对象
     * @return PageRequest对象
     */
    public static PageRequest getPageRequestByTableDataQueryCondition(TableDataQueryCondition cond) {
        if (cond == null) {
            return null;
        }
        return cond.getPageRequest();
    }

	public TableDataQueryCondition() {
		super();
	}
	
	public TableDataQueryCondition(String rows, String page, String sidx, String sord) {
		super();
		this.rows = rows;
		this.page = page;
		this.sidx = sidx;
		this.sord = sord;
		this.startInx = String.valueOf((Integer.parseInt(page) - 1)* Integer.parseInt(rows));
	}
	public String getStartInx() {
		return startInx;
	}

	public void setStartInx(String startInx) {
		this.startInx = startInx;
	}

	public String getRows() {
		return rows;
	}
	
	public int getRowsNumber() {
		return Integer.parseInt(rows);
	}
	
	public void setRows(String rows) {
		this.rows = rows;
		this.startInx = String.valueOf((Integer.parseInt(page) - 1)* Integer.parseInt(rows));
	}
	public String getPage() {
		return page;
	}
	
	public int getPageNumber() {
		return Integer.parseInt(page);
	}
	
	public void setPage(String page) {
		this.page = page;
		this.startInx = String.valueOf((Integer.parseInt(page) - 1)* Integer.parseInt(rows));
	}
	public String getSidx() {
		return sidx;
	}
	public void setSidx(String sidx) {
		this.sidx = sidx;
	}
	public String getSord() {
		return sord;
	}
	public void setSord(String sord) {
		this.sord = sord;
	}
	public T getOtherCond() {
		return otherCond;
	}

	public void setOtherCond(T otherCond) {
		this.otherCond = otherCond;
	}

	public String getOrderByCondition() {
		if (sidx == null || sidx.length() == 0) {
			return "";
		} else {
			return sidx + " " + sord;
		}
	}

	@SuppressWarnings("rawtypes")
	public Map getQueryCond() {
		return queryCond;
	}

	@SuppressWarnings("rawtypes")
	public void setQueryCond(Map queryCond) {
		this.queryCond = queryCond;
	}


	public List<SimpleExpression> getQueryCondSimple() {
		return queryCondSimple;
	}

	public void setQueryCondSimple(List<SimpleExpression> queryCondSimple) {
		this.queryCondSimple = queryCondSimple;
	}

	@Override
	public String toString() {
		return "TableDataQueryCondition [rows=" + rows + ", page=" + page + ", startInx=" + startInx + ", sidx=" + sidx
				+ ", queryCond=" + queryCond + ", sord=" + sord + ", otherCond=" + otherCond + "]";
	}
	
}
